package com.yami.shop.listener;

import cn.hutool.core.collection.CollUtil;
import com.yami.shop.bean.enums.StockBillType;
import com.yami.shop.bean.event.StockBillChangeEvent;
import com.yami.shop.bean.model.StockBillLog;
import com.yami.shop.service.StockBillLogService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * @Author lth
 * @Date 2021/9/22 9:05
 */
@Slf4j
@Component("StockBillChangeListener")
@AllArgsConstructor
public class StockBillChangeListener {

    private final StockBillLogService stockBillLogService;

    @EventListener(StockBillChangeEvent.class)
    public void stockBillChangeListener(StockBillChangeEvent event) {
        StockBillLog stockBillLog = event.getStockBillLog();
        if (CollUtil.isEmpty(event.getSkuList())) {
            return;
        }
        if (Objects.equals(stockBillLog.getStockBillType(), StockBillType.INITIALIZE.value())) {
            stockBillLogService.initialize(stockBillLog, event.getSkuList());
            return;
        }
        if (Objects.equals(stockBillLog.getStockBillType(), StockBillType.EDIT_OUTBOUND.value())) {
            stockBillLogService.generateStockLogOfEditInfo(stockBillLog, event.getSkuList());
            return;
        }
        if (Objects.equals(stockBillLog.getStockBillType(), StockBillType.EDIT_STORAGE.value())) {
            stockBillLogService.generateStockLogOfEditInfo(stockBillLog, event.getSkuList());
            return;
        }
        if (Objects.equals(stockBillLog.getStockBillType(), StockBillType.PROFIT_STORAGE.value()) || Objects.equals(stockBillLog.getStockBillType(), StockBillType.LOSS_OUTBOUND.value())) {
            stockBillLogService.generateStockLogOfTakeStock(stockBillLog, event.getSkuList());
        }
    }

}
